<?php
/*
 * 受访分析相关
 * copyright:living10
 */
class RespAnalysis_model extends CI_Model{
 	public function __construct()
 	{
 		$this->load->database();
 		$this->load->library('session');
 		$this->load->library('Tools');
 	}
	//来路分析
	public function showRespPage($sdate,$edate)
	{
		$int_sdate = strtotime($sdate);
		$int_edate = strtotime($edate." +1 day");
		$where = " ";
		$sql = "SELECT sum(stopTime) as stopTimes,count(arriveCount) as total_arrive,searchEngine,sum(isbad)".
				" as badclicks,sum(if(firstTime>(UNIX_TIMESTAMP()-86400),1,0)) as isnew,arriveRefer,ip,vid from tj_stat where  ".
				" wid=".$this->session->userdata('wid')." and arriveTime>=".$int_sdate." and arriveTime<=".$int_edate.$where.
				" GROUP BY arriveRefer,vid,ip";
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		$data = array();
		$i =0 ;
		foreach($arr as $kk=>$vv)
		{
			$vv['arriveRefer'] = str_replace("http:","",$vv['arriveRefer']);
			$key = $vv['arriveRefer'];
			@$data[$key]['short_arriveRefer']= $this->tools->cut_str($key,40);
			@$data[$key]['tmp_vids'][]= $vv['vid'];
			@$data[$key]['tmp_ips'][]= $vv['ip'];
			@$data[$key]['total_stopTimes'] +=$vv['stopTimes'];
			@$data[$key]['total_arrives'] +=$vv['total_arrive'];
			@$data[$key]['total_isnew'] +=$vv['isnew'];
			@$data[$key]['key'] = ++$i;
		}
		if($data)
		{
			foreach($data as $k=>$v)
			{
				$data[$k]['avg_arriveTimes'] = round($v['total_stopTimes']/$v['total_arrives'],2);
				$data[$k]['total_ips'] = count(array_unique($v['tmp_ips']));
				$data[$k]['total_vids'] = count(array_unique($v['tmp_vids']));
				unset($data[$k]['tmp_ips']);
				unset($data[$k]['tmp_vids']);
			}
		}

		$result_arr = array();
		$result_arr['data'] = $data;

		return $result_arr;
	}

	//初访页面
	public function showFirst($sdate,$edate)
	{
		$int_sdate = strtotime($sdate);

		$int_edate = strtotime($edate." +1 day");
		$where = " and arriveCount=1 ";
		$sql = "SELECT sum(stopTime) as stopTimes,count(arriveCount) as total_arrive,searchEngine,sum(isbad)".
				" as badclicks,sum(if(firstTime>(UNIX_TIMESTAMP()-86400),1,0)) as isnew,arriveRefer,ip,vid from tj_stat where  ".
				" wid=".$this->session->userdata('wid')." and arriveTime>=".$int_sdate." and arriveTime<=".$int_edate.$where.
				" GROUP BY refer,vid,ip";
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		$data = array();

		foreach($arr as $kk=>$vv)
		{
			$key = $vv['arriveRefer'];
			@$data[$key]['short_arriveRefer']= $this->tools->cut_str($key,40);
			@$data[$key]['tmp_vids'][]= $vv['vid'];
			@$data[$key]['tmp_ips'][]= $vv['ip'];
			@$data[$key]['total_stopTimes'] +=$vv['stopTimes'];
			@$data[$key]['total_arrives'] +=$vv['total_arrive'];
			@$data[$key]['total_isnew'] +=$vv['isnew'];
		}
		if($data)
		{
			foreach($data as $k=>$v)
			{
				$data[$k]['avg_arriveTimes'] = round($v['total_stopTimes']/$v['total_arrives'],2);
				$data[$k]['total_ips'] = count(array_unique($v['tmp_ips']));
				$data[$k]['total_vids'] = count(array_unique($v['tmp_vids']));
				unset($data[$k]['tmp_ips']);
				unset($data[$k]['tmp_vids']);
			}
		}
		$result_arr['data'] = $data;
		return $result_arr;
	}

	//站内访问
	public function showInner($sdate,$edate)
	{
		$int_sdate = strtotime($sdate);

		$int_edate = strtotime($edate." +1 day");
		$where = " and keyword is null and arriveCount>1 ";
		$sql = "SELECT sum(stopTime) as stopTimes,count(arriveCount) as total_arrive,searchEngine,sum(isbad)".
				" as badclicks,sum(if(firstTime>(UNIX_TIMESTAMP()-86400),1,0)) as isnew,arriveRefer,ip,vid from tj_stat where  ".
				" wid=".$this->session->userdata('wid')." and arriveTime>=".$int_sdate." and arriveTime<=".$int_edate.$where.
				" GROUP BY refer,vid,ip";
		$query = $this->db->query($sql);
		$arr = $query->result_array();
		$data = array();
		$i=0;
		foreach($arr as $kk=>$vv)
		{
			$key = $vv['arriveRefer'];
			@$data[$key]['short_arriveRefer']= $this->tools->cut_str($key,40);
			@$data[$key]['tmp_vids'][]= $vv['vid'];
			@$data[$key]['tmp_ips'][]= $vv['ip'];
			@$data[$key]['total_stopTimes'] +=$vv['stopTimes'];
			@$data[$key]['total_arrives'] +=$vv['total_arrive'];
			@$data[$key]['total_isnew'] +=$vv['isnew'];
			@$data[$key]['key'] = ++$i;
		}
		if($data)
		{
			foreach($data as $k=>$v)
			{
				$data[$k]['avg_arriveTimes'] = round($v['total_stopTimes']/$v['total_arrives'],2);
				$data[$k]['total_ips'] = count(array_unique($v['tmp_ips']));
				$data[$k]['total_vids'] = count(array_unique($v['tmp_vids']));
				unset($data[$k]['tmp_ips']);
				unset($data[$k]['tmp_vids']);
			}
		}
		$result_arr['data'] = $data;
		return $result_arr;
	}


	function make_xml($data,$caption_name,$value_name)
 	{
		$str = '<?xml version="1.0" encoding="gb2312"?>';
		$str .= '<chart caption="'.$caption_name.'" baseFontSize="14" palette="2" animation="1" formatNumberScale="0" pieSliceDepth="30" startingAngle="125" showValues="1" showPercentInToolTip="1" showLabels="1" showLegend="1" formatNumber="1">';
		foreach($data as $k=>$v)
		{
			$str .= '<set label="'.$k.'" value="'.$v[$value_name].'" isSliced="1"/>';
		}
		$str .='</chart>';
		return $str;
	}
}
?>
